//
//  ZBNotificationBar.m
//  ZBNotificationBar
//
//  Created by xzb on 2018/7/21.
//

#import "ZBNotificationBar.h"

@interface ZBNotificationBar ()

@property (nonatomic, assign) CGPoint oriCenter; //初始位置

@end

@implementation ZBNotificationBar

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 8;
        self.layer.masksToBounds = YES;
        //上滑关闭
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
        [self addGestureRecognizer:panGesture];
    }
    return self;
}

- (void)panGesture:(UIPanGestureRecognizer *)sender
{
    CGPoint movePoint = [sender translationInView:self];
    CGPoint center = self.center;
    
    if (movePoint.y <= 0) {
        center.y += movePoint.y;
    }
    self.center = center;
    [sender setTranslation:CGPointZero inView:self];
    
    if (sender.state == UIGestureRecognizerStateEnded) {
        [self hide:YES afterDelay:0];
    }
}

#pragma mark - api

+ (instancetype)notificationBar
{
    return [ZBNotificationBar notificationBarAddedTo:nil subView:nil];
}

+ (instancetype)notificationBarAddedTo:(UIView *)superView subView:(UIView *)subView
{
    ZBNotificationBar *barView = [[ZBNotificationBar alloc] init];
    if (!superView) {
        superView = [UIApplication sharedApplication].keyWindow;
    }
    [superView addSubview:barView];
    if (subView) {
        [barView makeLayout];
        [barView addSubview:subView];
        subView.bounds = barView.bounds;
    }
    return barView;
}

- (void)makeLayout
{
    [self makeLayoutWithInsets:UIEdgeInsetsMake(0, 6, 0, 6) height:64];
}

- (void)makeLayoutWithInsets:(UIEdgeInsets)insets height:(CGFloat)height
{
    self.frame = CGRectMake(insets.left, [self getSafeInsets].top + insets.top, CGRectGetWidth([UIScreen mainScreen].bounds), height);
}

-(UIEdgeInsets)getSafeInsets
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
    if (@available(iOS 11, *)) {
        return self.safeAreaInsets;
    }
    else {
        return UIEdgeInsetsZero;
    }
#else
    return UIEdgeInsetsZero;
#endif
}


- (void)show:(BOOL)animated hideAfterDelay:(NSTimeInterval)delay
{
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    [self layoutIfNeeded];
    
    self.oriCenter = self.center;
    
    self.center = CGPointMake(self.oriCenter.x, self.oriCenter.y - CGRectGetHeight(self.frame));
    [UIView animateWithDuration:animated ? 0.5 : 0 animations:^{
        self.center = self.oriCenter;
    }];
    
    if (delay > 0) {
        [self hide:YES afterDelay:delay];
    }
}

- (void)show:(BOOL)animated
{
    [self show:animated hideAfterDelay:0];
}

- (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (animated) {
            [UIView animateWithDuration:0.25 animations:^{
                self.center = CGPointMake(self.oriCenter.x, self.oriCenter.y - CGRectGetHeight(self.frame));
            } completion:^(BOOL finished) {
                if (finished) [self removeFromSuperview];
            }];
            return;
        }
        [self removeFromSuperview];
    });
}
#pragma mark - event Response

#pragma mark - private methods

#pragma mark - getter And setter

@end
